// @Author EthanScriptOn
// @Desc
package redis_operator

import (
	"fmt"
	"gitee.com/fatzeng/srf_switch_basic_components/common/identity"
	"gitee.com/fatzeng/srf_switch_expansion_package/connect"
	"gitee.com/fatzeng/srf_switch_expansion_package/extend_config"
	"gitee.com/fatzeng/srf_switch_expansion_package/extend_param_collector"
	"gitee.com/fatzeng/srf_switch_stream_bridge/framework/actuator"
)

type RedisQueryOperator struct {
	*identity.IdentityInformationCarrier
}

func (r *RedisQueryOperator) Execute(ctx *actuator.SrfSwitchBridgeActuatorContext, configIdentifier interface{}, structureParam interface{}) (interface{}, error) {
	params, ok := structureParam.(*extend_param_collector.RedisQueryParams)
	if !ok {
		return nil, fmt.Errorf("structureParam is not *RedisParams")
	}
	if !params.IsValid() {
		return nil, fmt.Errorf("params is not valid")
	}
	redisQueryDB, err := connect.GetRedisQueryDB()
	if err != nil {
		return nil, err
	}

	operatorResponse := make([]*extend_config.RedisQueryOperatorResponse, 0)
	for _, param := range params.Param {
		result, err := redisQueryDB.Get(params.Ctx, param).Result()
		resp := new(extend_config.RedisQueryOperatorResponse)
		resp.Err = err
		resp.Result = result
		operatorResponse = append(operatorResponse, resp)
	}
	return operatorResponse, nil
}
